﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using micsun.Logic;
using micsun.SqlDom;
using micsun.Data;
using System.Data.Common;

namespace commfuncs
{
    public class Misc
    {
        /// <summary>
        /// 通过关键字获取sp表的文字描述
        /// </summary>
        /// <param name="keyid"></param>
        /// <param name="target"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string GetSpTextById(int keyid,string KeyName, string target, string tableName)
        {
            string sql = "select " + target + " from  " + tableName + " where " + KeyName + "=" + keyid;
            //"select c.Id,c.SsrKey,c.Portion,s.StateName from map_content c,sp_ssrstate s where s.StateID=c.SsrState";
            object strobj =  micsunDB.CreateDataAccessor().ExecuteScalar(sql, null).ToString();
            if (strobj != DBNull.Value)
            {
                return strobj.ToString();
            }
            return strobj.ToString();
        }
        /// <summary>
        /// 设置所有文本控件的Enabled属性
        /// </summary>
        /// <param name="isEnabled"></param>
        /// <param name="owner">所属容器控件</param>
        public static void setTextEnabled(bool isEnabled, System.Web.UI.Control owner)
        {
            foreach (System.Web.UI.Control txt in owner.Controls)
            {
                switch (txt.GetType().Name)
                {
                    case "TextBox":
                        ((System.Web.UI.WebControls.TextBox)(txt)).Enabled = isEnabled;
                        break;
                    case "DropDownList":
                        ((System.Web.UI.WebControls.DropDownList)(txt)).Enabled = isEnabled;
                        break;

                }
            }
        }

        /// <summary>
        /// 清除所有的文本控件中的字符
        /// </summary>
        public static void clearText(System.Web.UI.Control owner)
        {
            foreach (System.Web.UI.Control txt in owner.Controls)
            {
                if (txt.GetType().Name == "TextBox")
                {
                    ((System.Web.UI.WebControls.TextBox)(txt)).Text = string.Empty;
                }
            }
        }
       
    }
    public class C_DBV
    {
        // 数据库连接字符串 ---------------
        protected string m_dbConnection = string.Empty;

        public string dbConnection
        {
            get { return m_dbConnection; }
            set { m_dbConnection = value; }
        }

        public C_DBV()
        {
            m_dbConnection = micsunDB.ConnectionString;
        }

        /// <summary>
        /// 查询数据，返回数据列表
        /// </summary>
        /// <param name="selectCmd">SQL查询语句</param>
        /// <returns>数据集</returns>
        public DataTable GetDataTable(string selectCmd) // 查询数据，并将结果以DataTable数据类型返回
        {
            DataTable dt = new DataTable();
            try
            {
                micsunDB.CreateDataAdapter(selectCmd, null).Fill(dt);
            } // end try
            catch (Exception e)
            {
                throw new Exception("Error in CT_NVS:GetDataTable()-> " + e.ToString());
            }
            finally
            {
            }
            return dt;
        }

        public bool RunSql(string sql)// 运行SQL语句
        {
            bool res = false;
            try
            {
                int i = micsunDB.CreateDataAccessor().ExecuteNonQuery(sql, null);
                res = true;
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }
            return res;
        }

        public object GetOnlyValue(string sql)// 从数据库中读取指定字段的值
        {
            object obj;
            try
            {
                obj = micsunDB.CreateDataAccessor().ExecuteScalar(sql, null);
            }
            catch
            {
                obj = null;
            }
            return obj;
        }

        public IDbTransaction CreateTran(out LogicDB ldb)
        {
            ldb = new LogicDB();
            ldb.Connection.Open();
            IDbTransaction tran = ldb.Connection.BeginTransaction();
            ldb.DAS.Transaction = tran;
            return tran;
        }

        public void RunTran(LogicDB ldb, string sql)
        {
            ldb.CreateDataAccessor().ExecuteNonQuery(sql);
        }

       
    }

}
